﻿
Partial Class Sistema_InternadosPersonasEditar
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Request.QueryString("idinternado") = Nothing And String.IsNullOrEmpty(hdidinternado.Value) Then
            Response.Redirect("Internados.aspx")
        End If
        If Page.IsPostBack = False Then
            hdidInternadoPersona.Value = Request.QueryString("idInternadoPersona")
            hdidinternado.Value = Request.QueryString("idinternado")
            'llenarCombosPersonas()
            If String.IsNullOrEmpty(hdidInternadoPersona.Value) Then
                DtvPersona.ChangeMode(DetailsViewMode.Insert)
            Else
                DtvPersona.ChangeMode(DetailsViewMode.ReadOnly)
                cargardatos()
            End If
        End If
    End Sub
    Sub cargardatos()
        Using bd As New BdHogaresDataContext
            Dim a = From c In bd.Web_internadosPersonas
                    Where c.idInternadoPersona = hdidInternadoPersona.Value
                    Select c

            DtvPersona.ChangeMode(DetailsViewMode.Edit)
            DtvPersona.DataSource = a
            DtvPersona.DataBind()

        End Using

    End Sub

#Region "Lllenar combos personas"
    Function TreaeCategorias(ByVal categoria As String) As IEnumerable
        Dim result As IEnumerable
        Using bd As New BdHogaresDataContext
            Dim a = From c In bd.Web_Parametros
                    Where c.Categoria = categoria
                    Select c.ValorOpcion, c.NombreOpcion

            result = a
        End Using
        Return result
    End Function
    Sub LLenarCombo(ByVal DtvDatos As DetailsView, ByVal combo As String, ByVal categoria As String)
        Dim objeto As New DropDownList()
        objeto = CType(DtvDatos.FindControl(combo), DropDownList)
        objeto.DataSource = TreaeCategorias(categoria)
        objeto.DataTextField = "NombreOpcion"
        objeto.DataValueField = "ValorOpcion"
        objeto.DataBind()
    End Sub

    Sub llenarCombosPersonas()
        LLenarCombo(DtvPersona, "ddlTipoDocumento", "tipoDocumento")
    End Sub
#End Region


    Protected Sub DtvPersona_DataBound(sender As Object, e As System.EventArgs) Handles DtvPersona.DataBound

    End Sub

    Protected Sub DtvPersona_DataBinding(sender As Object, e As System.EventArgs) Handles DtvPersona.DataBinding
        'LLenarCombo(DtvPersona, "ddlTipoDocumento", "tipoDocumento")

    End Sub

    Protected Sub DtvPersona_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs) Handles DtvPersona.ItemCommand
        Select Case e.CommandName
            Case "CancelInsert"
                Response.Redirect("InternadosPersonas.aspx?idinternado=" & hdidinternado.Value)
            Case "InsertBtn"
                If Page.IsValid Then
                    guardar()
                End If
        End Select

    End Sub

    Protected Sub cValidator_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim validador As CustomValidator = CType(DtvPersona.FindControl("cValidator"), CustomValidator)
        validador.ErrorMessage = "Este es un error de prueba"
        validador.Enabled = True
        args.IsValid = False
    End Sub

    Private Sub guardar()
        Throw New NotImplementedException
    End Sub

End Class
